package com.dc.chat.app.biz.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dc.chat.app.biz.GrpBiz;
import com.dc.chat.app.service.FileService;
import com.dc.chat.app.service.mp.*;
import com.dc.chat.entity.dao.*;
import com.dc.chat.entity.redisCatch.LoginUser;
import com.dc.chat.entity.to.ResponseBean;
import com.dc.chat.entity.to.grp.GetGrpVerifResp;
import com.dc.chat.entity.to.grp.GetMyApplyResp;
import com.dc.chat.entity.to.grp.GetMyGrpResp;
import com.dc.chat.entity.to.grp.GetMyMGrpResp;
import com.dc.chat.interceptor.security.Security;
import com.dc.chat.interceptor.security.SecurityInterceptor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.impl.DefaultJwtParser;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.lang.model.element.VariableElement;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class GrpBizImpl implements GrpBiz {
    @Resource
    SimpMessagingTemplate simpMessagingTemplate;
    @Resource
    FileService fileService;
    @Resource
    UserService userService;
    @Resource
    GrpBizService.GrpMapper grpMapper;
    @Resource
    RedisTemplate redisTemplate;
    @Resource
    GrpDetailService grpDetailService;
    @Resource
    GrpDetailService.GrpDetailMapper grpDetailMapper;
    @Resource
    GrpApplyService grpApplyService;
    @Resource
    GrpApplyService.GrpApplyMapper grpApplyMapper;
    @Resource
    UserService.UserMapper userMapper;
    @Transactional
    @Override
    public ResponseBean addGrp(MultipartFile file, String grpName, String grpDescription) throws IOException {
        Grp grp=new Grp();
        if(grpName==null){
            return ResponseBean.failed("群名不能为空");
        }else if(grpName.replaceAll("\\s+","").equals("")){
            return ResponseBean.failed("群名不能为空字符");
        }else if(grpDescription==null){
            return ResponseBean.failed("群描述不能为空");
        }else if(grpDescription.replaceAll("\\s+","").equals("")){
            return ResponseBean.failed("群描述不能为空字符");
        }
        if(file==null){
            grp.setAvatar("https://img0.baidu.com/it/u=2472550237,3508413336&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400");
        }else{
            String originalFilename = file.getOriginalFilename();
            if(!originalFilename.matches(".*\\.(png|jpg|jpeg|webp|gif|ico)$")){
                return ResponseBean.failed("头像格式不是图片");
            }
            ResponseBean<String> upload = fileService.upload(file);
            String data = upload.getData();
            grp.setAvatar(data);
        }
        grp.setName(grpName);
        grp.setDescription(grpDescription);
        grp.setCreateTime(LocalDateTime.now());
        String username = SecurityInterceptor.threadLocal.get();
        User user = userService.selectByUserName(username);
        grp.setUserId(user.getId());
        int insert = grpMapper.insert(grp);
        if(insert==1){
            GrpDetail grpDetail=new GrpDetail();
            grpDetail.setGrpId(grp.getId());
            grpDetail.setJoinTime(LocalDateTime.now());
            grpDetail.setUserId(user.getId());
            grpDetailMapper.insert(grpDetail);
            //通知自己
            Map map=new HashMap();
            map.put("createGrp",true);
            ObjectMapper objectMapper=new ObjectMapper();
            GetMyGrpResp getMyGrpResp=new GetMyGrpResp();
            getMyGrpResp.setNoRead(0);
            getMyGrpResp.setAvatar(grp.getAvatar());
            getMyGrpResp.setName(grp.getName());
            getMyGrpResp.setBack(null);
            getMyGrpResp.setId(grp.getId());
            getMyGrpResp.setMasterId(user.getId());
            String loginToken = (String) redisTemplate.opsForHash().entries("login").get(username);
            if(loginToken!=null){
                simpMessagingTemplate.convertAndSend("/u/"+loginToken,objectMapper.writeValueAsString(getMyGrpResp),map);
            }
            return ResponseBean.success("创建成功",grp);
        }else{
            return ResponseBean.failed("创建失败");
        }
    }

    @Override
    public void setActiveGrp(Integer id) {
        String username = SecurityInterceptor.threadLocal.get();
        User user = userService.selectByUserName(username);
        //群未读消息置为0
        GrpDetail grpDetail = new GrpDetail();
        grpDetail.setNoRead(0);
        grpDetailMapper.update(grpDetail,Wrappers.lambdaUpdate(new GrpDetail()).eq(GrpDetail::getUserId,user.getId()).eq(GrpDetail::getGrpId,id));
        Map<String,String> loginTokenMap = redisTemplate.opsForHash().entries("login");
        String token = loginTokenMap.get(username);
        LoginUser loginUser = (LoginUser) redisTemplate.opsForValue().get("login#1#" + username + "#" + token);
        loginUser.setActiveUserId(null);
        loginUser.setActiveGrpId(id);
        redisTemplate.opsForValue().set("login#1#" + username + "#" + token,loginUser);
    }

    @Override
    public ResponseBean<List<GetMyMGrpResp>> getMyMGrp() {
        String username = SecurityInterceptor.threadLocal.get();
        User user = userService.selectByUserName(username);
        List<Grp> grps = grpMapper.selectList(Wrappers.lambdaQuery(new Grp()).eq(Grp::getUserId, user.getId()));
        List<GetMyMGrpResp> resp=new ArrayList<>();
        for (Grp grp : grps) {
            GetMyMGrpResp getMyMGrpResp=new GetMyMGrpResp();
            getMyMGrpResp.setId(grp.getId());
            getMyMGrpResp.setName(grp.getName());
            getMyMGrpResp.setCreateTime(grp.getCreateTime());
            getMyMGrpResp.setDescription(grp.getDescription());
            getMyMGrpResp.setAvatar(grp.getAvatar());
            //群人数
            int size = grpDetailMapper.selectList(Wrappers.lambdaQuery(new GrpDetail()).eq(GrpDetail::getGrpId, grp.getId())).size();
            getMyMGrpResp.setPeopleCount(size);
            resp.add(getMyMGrpResp);
        }
        return ResponseBean.success("查询成功",resp);
    }

    @Override
    public ResponseBean delMyMGrp(Integer id) {
        String username = SecurityInterceptor.threadLocal.get();
        User user = userService.selectByUserName(username);
        int delete = grpMapper.delete(Wrappers.lambdaQuery(new Grp()).eq(Grp::getUserId, user.getId()).eq(Grp::getId, id));
        if(delete==0){
            return ResponseBean.failed("删除失败");
        }else{
            //通知所有在线的群员
            List<GrpDetail> grpDetails = grpDetailMapper.selectList(Wrappers.lambdaQuery(new GrpDetail()).eq(GrpDetail::getGrpId, id));
            List<Integer> ids = grpDetails.stream().map(i -> i.getUserId()).collect(Collectors.toList());
            List<User> users = userMapper.selectBatchIds(ids);
            Set<String> userNames = users.stream().map(i -> i.getUserName()).collect(Collectors.toSet());
            Map<String,String> userTokenMap = redisTemplate.opsForHash().entries("login");
            for (String userName : userNames) {
                String token = userTokenMap.get(userName);
                if(token!=null){
                    Map map=new HashMap();
                    map.put("grpDie",true);
                    map.put("id",id);
                    simpMessagingTemplate.convertAndSend("/u/"+token,"",map);
                }
            }
            //删除群员
            grpDetailMapper.delete(Wrappers.lambdaQuery(new GrpDetail()).eq(GrpDetail::getGrpId,id));
            //删除申请
            grpApplyMapper.delete(Wrappers.lambdaQuery(new GrpApply()).eq(GrpApply::getGrpId,id));

            return ResponseBean.success("删除成功",null);
        }
    }

    @Override
    public ResponseBean<GetMyMGrpResp> searchGrp(Integer id) {
        Grp grp = grpMapper.selectOne(Wrappers.lambdaQuery(new Grp()).eq(Grp::getId, id));
        if(grp==null){
            return ResponseBean.failed("");
        }else{
            GetMyMGrpResp getMyMGrpResp=new GetMyMGrpResp();
            getMyMGrpResp.setId(id);
            getMyMGrpResp.setName(grp.getName());
            getMyMGrpResp.setDescription(grp.getDescription());
            getMyMGrpResp.setAvatar(grp.getAvatar());
            getMyMGrpResp.setCreateTime(grp.getCreateTime());
            //T获取群人数
            int size = grpDetailMapper.selectList(Wrappers.lambdaQuery(new GrpDetail()).eq(GrpDetail::getGrpId, grp.getId())).size();
            getMyMGrpResp.setPeopleCount(size);
            return ResponseBean.success("",getMyMGrpResp);
        }
    }
    @Override
    public ResponseBean joinGrp(String message, Integer id) {
        String username = SecurityInterceptor.threadLocal.get();
        User user = userService.selectByUserName(username);
        Grp grp = grpMapper.selectById(id);
        if(grp==null){
            return ResponseBean.failed("群聊不存在");
        }
        //查看是否在群里
        GrpDetail grpDetail = grpDetailMapper.selectOne(Wrappers.lambdaQuery(new GrpDetail()).eq(GrpDetail::getGrpId, id).eq(GrpDetail::getUserId, user.getId()));
        if(grpDetail!=null){
            return ResponseBean.failed("你已经在群组里，不能发送加入群聊请求");
        }
        //查看是否之前发送过请求还没验证  就把message拼接上
        GrpApply grpApply = grpApplyMapper.selectOne(Wrappers.lambdaQuery(new GrpApply())
                .eq(GrpApply::getGrpId, id)
                .eq(GrpApply::getUserId, user.getId())
                .eq(GrpApply::getStatus, 0)
        );
        if(grpApply!=null){
            grpApply.setDescription(grpApply.getDescription()+" "+message);
            grpApply.setCreateTime(LocalDateTime.now());
            grpApplyMapper.updateById(grpApply);
            //发送给群主消息
            Integer userId = grp.getUserId();
            User byId = userService.getById(userId);
            String userName = byId.getUserName();
            Map<String,String> userTokenMap = redisTemplate.opsForHash().entries("login");
            boolean contains = userTokenMap.keySet().contains(userName);
            if(contains){
                Map map=new HashMap();
                map.put("somebodyJoinGrp",true);
                simpMessagingTemplate.convertAndSend("/u/"+userTokenMap.get(userName),"用户名为"+userName+"的用户申请加入"+grp.getName()+" 群聊",map);
            }
            return ResponseBean.success("",null);
        }
        //没发送过请求就插入数据
        GrpApply grpApply1=new GrpApply();
        grpApply1.setGrpId(id);
        grpApply1.setUserId(user.getId());
        grpApply1.setStatus(0);
        grpApply1.setDescription(message);
        grpApply1.setCreateTime(LocalDateTime.now());
        grpApplyMapper.insert(grpApply1);
        //发送给群主消息
        Integer userId = grp.getUserId();
        User byId = userService.getById(userId);
        String userName = byId.getUserName();
        Map<String,String> userTokenMap = redisTemplate.opsForHash().entries("login");
        boolean contains = userTokenMap.keySet().contains(userName);
        if(contains){
            Map map=new HashMap();
            map.put("somebodyJoinGrp",true);
            simpMessagingTemplate.convertAndSend("/u/"+userTokenMap.get(userName),"用户名为"+userName+"的用户申请加入"+grp.getName()+" 群聊",map);
        }
        return ResponseBean.success("",null);
    }

    @Override
    public ResponseBean getGrpVerif() {
        String username = SecurityInterceptor.threadLocal.get();
        User user = userService.selectByUserName(username);
        //查询自己所有的群
        List<Grp> grps = grpMapper.selectList(Wrappers.lambdaQuery(new Grp()).eq(Grp::getUserId, user.getId()));
        Set<Integer> grpIds = grps.stream().map(i -> i.getId()).collect(Collectors.toSet());
        if(grpIds.isEmpty()){
            return ResponseBean.success("",null);
        }else{
            List<GrpApply> grpApplies = grpApplyMapper.selectList(Wrappers.lambdaQuery(new GrpApply())
                    .in(GrpApply::getGrpId, grpIds)
                    .orderByDesc(GrpApply::getCreateTime)
            );
            if(grpApplies.isEmpty()){
                return ResponseBean.success("",null);
            }
            Set<Integer> userIds = grpApplies.stream().map(i -> i.getUserId()).collect(Collectors.toSet());
            List<User> users = userMapper.selectBatchIds(userIds);
            Map<Integer, User> userIdUserMap = users.stream().collect(Collectors.toMap(User::getId, i -> i));

            Set<Integer> grpIds1 = grpApplies.stream().map(i -> i.getGrpId()).collect(Collectors.toSet());
            Map<Integer, Grp> grpIdGrpMap = grps.stream().filter(i -> grpIds1.contains(i.getId())).collect(Collectors.toMap(Grp::getId, i -> i));
            List<GetGrpVerifResp> resp=new ArrayList<>();
            for (GrpApply grpApply : grpApplies) {
                GetGrpVerifResp getGrpVerifResp=new GetGrpVerifResp();
                getGrpVerifResp.setId(grpApply.getId());
                getGrpVerifResp.setGrpId(grpApply.getGrpId());
                getGrpVerifResp.setApplyTime(grpApply.getCreateTime());
                getGrpVerifResp.setCheckTime(grpApply.getCheckTime());
                getGrpVerifResp.setDescription(grpApply.getDescription());
                getGrpVerifResp.setStatus(grpApply.getStatus());

                Grp grp = grpIdGrpMap.get(grpApply.getGrpId());
                getGrpVerifResp.setGrpName(grp.getName());
                getGrpVerifResp.setGrpAvatar(grp.getAvatar());

                User user1 = userIdUserMap.get(grpApply.getUserId());
                getGrpVerifResp.setUserName(user1.getUserName());
                getGrpVerifResp.setUserNickname(user1.getNickName());
                getGrpVerifResp.setUserAvatar(user1.getAvatar());

                resp.add(getGrpVerifResp);

            }
            return ResponseBean.success("",resp);
        }
    }

    @Override
    public ResponseBean agreeJoinGrp(Integer id) throws JsonProcessingException {
        String username = SecurityInterceptor.threadLocal.get();
        User user = userService.selectByUserName(username);

        GrpApply grpApply = grpApplyMapper.selectOne(Wrappers.lambdaQuery(new GrpApply()).eq(GrpApply::getId,id));
        if(grpApply==null){
            return ResponseBean.failed("没有此条记录");
        }else{
            Integer grpId = grpApply.getGrpId();
            Grp grp = grpMapper.selectById(grpId);
            if(!grp.getUserId().equals(user.getId())){
                return ResponseBean.failed("你无权操作");
            }else{
                if(grpApply.getStatus()!=0){
                    return ResponseBean.failed("该请求不是待审核状态");
                }else{
                    //群里填记录
                    GrpDetail grpDetail=new GrpDetail();
                    grpDetail.setUserId(grpApply.getUserId());
                    grpDetail.setGrpId(grp.getId());
                    grpDetail.setJoinTime(LocalDateTime.now());
                    grpDetailMapper.insert(grpDetail);
                    //改变该请求状态
                    grpApply.setCheckTime(LocalDateTime.now());
                    grpApply.setStatus(1);
                    grpApplyMapper.updateById(grpApply);
                    //告诉添加的人  把群聊信息发给他
                    Integer userId = grpApply.getUserId();
                    User user1 = userMapper.selectById(userId);
                    String userName = user1.getUserName();
                    Map<String,String> userTokenMap = redisTemplate.opsForHash().entries("login");
                    String token = userTokenMap.get(userName);
                    if(token!=null){
                        Map map=new HashMap();
                        map.put("enterGrp",true);
                        map.put("message","群主已同意你加入"+grp.getName()+" 群聊");
                        ObjectMapper objectMapper=new ObjectMapper();
                        GetMyGrpResp getMyGrpResp=new GetMyGrpResp();
                        getMyGrpResp.setNoRead(0);
                        getMyGrpResp.setAvatar(grp.getAvatar());
                        getMyGrpResp.setName(grp.getName());
                        getMyGrpResp.setBack(null);
                        getMyGrpResp.setId(grp.getId());
                        simpMessagingTemplate.convertAndSend("/u/"+token,objectMapper.writeValueAsString(getMyGrpResp),map);
                    }
                    return ResponseBean.success("操作成功",null);
                }
            }
        }
    }

    @Override
    public ResponseBean disAgreeJoinGrp(Integer id) {
        String username = SecurityInterceptor.threadLocal.get();
        User user = userService.selectByUserName(username);

        GrpApply grpApply = grpApplyMapper.selectOne(Wrappers.lambdaQuery(new GrpApply()).eq(GrpApply::getId,id));
        if(grpApply==null){
            return ResponseBean.failed("没有此条记录");
        }else{
            Integer grpId = grpApply.getGrpId();
            Grp grp = grpMapper.selectById(grpId);
            if(!grp.getUserId().equals(user.getId())){
                return ResponseBean.failed("你无权操作");
            }else{
                if(grpApply.getStatus()!=0){
                    return ResponseBean.failed("该请求不是待审核状态");
                }else{
                    //改变该请求状态
                    grpApply.setCheckTime(LocalDateTime.now());
                    grpApply.setStatus(2);
                    grpApplyMapper.updateById(grpApply);
                    return ResponseBean.success("操作成功",null);
                }
            }
        }
    }

    @Override
    public ResponseBean getMyApply() {
        String username = SecurityInterceptor.threadLocal.get();
        User user = userService.selectByUserName(username);
        List<GrpApply> grpApplies = grpApplyMapper.selectList(Wrappers.lambdaQuery(new GrpApply()).eq(GrpApply::getUserId, user.getId()));
        if(grpApplies.isEmpty()){
            return ResponseBean.success("",null);
        }else{
            Set<Integer> grpIds = grpApplies.stream().map(i -> i.getGrpId()).collect(Collectors.toSet());
            List<Grp> grps = grpMapper.selectBatchIds(grpIds);
            Map<Integer, Grp> grpIdGrpMap = grps.stream().collect(Collectors.toMap(i -> i.getId(), i -> i));
            List<GetMyApplyResp> resp=new ArrayList<>();
            for (GrpApply grpApply : grpApplies) {
                GetMyApplyResp getMyApplyResp=new GetMyApplyResp();
                getMyApplyResp.setAvatar(grpIdGrpMap.get(grpApply.getGrpId()).getAvatar());
                getMyApplyResp.setCheckTime(grpApply.getCheckTime());
                getMyApplyResp.setCreateTime(grpApply.getCreateTime());
                getMyApplyResp.setId(grpApply.getGrpId());
                getMyApplyResp.setStatus(grpApply.getStatus());
                getMyApplyResp.setName(grpIdGrpMap.get(grpApply.getGrpId()).getName());
                resp.add(getMyApplyResp);
            }
            return ResponseBean.success("查询成功",resp);
        }
    }

    @Override
    public ResponseBean getMyGrp() {
        String username = SecurityInterceptor.threadLocal.get();
        User user = userService.selectByUserName(username);
        List<GrpDetail> grpDetails = grpDetailMapper.selectList(Wrappers.lambdaQuery(new GrpDetail()).eq(GrpDetail::getUserId, user.getId()));
        if(grpDetails.isEmpty()){
            return ResponseBean.success("",null);
        }else{
            Set<Integer> grpIds = grpDetails.stream().map(i -> i.getGrpId()).collect(Collectors.toSet());
            List<Grp> grps = grpMapper.selectBatchIds(grpIds);
            Map<Integer, Grp> grpIdGrpMap = grps.stream().collect(Collectors.toMap(i -> i.getId(), i -> i));
            List<GetMyGrpResp> resp=new ArrayList<>();
            for (GrpDetail grpDetail : grpDetails) {
                GetMyGrpResp getMyGrpResp=new GetMyGrpResp();
                getMyGrpResp.setId(grpDetail.getGrpId());
                Grp grp = grpIdGrpMap.get(grpDetail.getGrpId());
                getMyGrpResp.setName(grp.getName());
                getMyGrpResp.setAvatar(grp.getAvatar());
                getMyGrpResp.setBack(grpDetail.getBack());
                getMyGrpResp.setNoRead(grpDetail.getNoRead());
                getMyGrpResp.setMasterId(grp.getUserId());
                resp.add(getMyGrpResp);
            }
            return  ResponseBean.success("",resp);
        }
    }
    @Resource
    GrpMessageService grpMessageService;
    @Resource
    GrpMessageService.GrpMessageMapper grpMessageMapper;
    @Override
    public ResponseBean getGrpMessage() {
        String username = SecurityInterceptor.threadLocal.get();
        User user = userService.selectByUserName(username);
        List<GrpDetail> grpDetails = grpDetailMapper.selectList(Wrappers.lambdaQuery(new GrpDetail()).eq(GrpDetail::getUserId, user.getId()));
        Set<Integer> grpIds = grpDetails.stream().map(i -> i.getGrpId()).collect(Collectors.toSet());
        Map<Integer,List<GrpMessage>> map=new HashMap();
        if(grpIds.isEmpty()){
            return ResponseBean.success("",map);
        }else{
            List<GrpMessage> grpMessages = grpMessageMapper.selectList(Wrappers.lambdaQuery(new GrpMessage()).in(GrpMessage::getGrpId, grpIds).orderByDesc(GrpMessage::getCreateTime));
            Map<Integer, List<GrpMessage>> collect = grpMessages.stream().collect(Collectors.groupingBy(GrpMessage::getGrpId));
            for (Integer grpId : grpIds) {
                if(!collect.containsKey(grpId)){
                    collect.put(grpId,new ArrayList<>());
                }
            }
            return ResponseBean.success("",collect);
        }
    }

    @Override
    public ResponseBean setGrpBack(Integer id, String back) {
        String username = SecurityInterceptor.threadLocal.get();
        User user = userService.selectByUserName(username);
        LambdaUpdateWrapper<GrpDetail> updateWrapper = Wrappers.lambdaUpdate(new GrpDetail());
        updateWrapper.set(GrpDetail::getBack,back);
        updateWrapper.eq(GrpDetail::getUserId,user.getId());
        updateWrapper.eq(GrpDetail::getGrpId,id);
        int update = grpDetailMapper.update(null, updateWrapper);
        if(update!=0){
            return ResponseBean.success("",null);
        }else{
            return ResponseBean.failed("");
        }
    }

    @Override
    public ResponseBean exitGrp(Integer id) {
        String username = SecurityInterceptor.threadLocal.get();
        User user = userService.selectByUserName(username);
        int delete = grpDetailMapper.delete(Wrappers.lambdaQuery(new GrpDetail()).eq(GrpDetail::getUserId, user.getId()).eq(GrpDetail::getGrpId,id));
        if(delete!=0){
            return ResponseBean.success("",null);
        }else{
            return ResponseBean.failed("");
        }
    }

    @Override
    public ResponseBean inviteUserJoinMyGrp(Integer id, String username) throws JsonProcessingException {
        String localUsername = SecurityInterceptor.threadLocal.get();
        User localUser = userService.selectByUserName(localUsername);

        User user = userService.selectByUserName(username);
        Grp grp = grpMapper.selectById(id);
        if(grp==null){
            return ResponseBean.failed("群不存在");
        }
        if(!grp.getUserId().equals(localUser.getId())){
            return ResponseBean.failed("你无权操作");
        }
        if(user==null){
            return ResponseBean.failed("该用户不存在");
        }else{
            GrpDetail grpDetail = grpDetailMapper.selectOne(Wrappers.lambdaQuery(new GrpDetail()).eq(GrpDetail::getGrpId, id).eq(GrpDetail::getUserId, user.getId()));
            if(grpDetail!=null){
                return ResponseBean.failed("此用户已经在群聊中");
            }
            GrpApply grpApply = grpApplyMapper.selectOne(Wrappers.lambdaQuery(new GrpApply()).eq(GrpApply::getUserId, user.getId()).eq(GrpApply::getGrpId, id).eq(GrpApply::getStatus, 0));
            if(grpApply!=null){
                grpApply.setStatus(1);
                grpApply.setCheckTime(LocalDateTime.now());
                grpApplyMapper.updateById(grpApply);
            }
            GrpDetail insert=new GrpDetail();
            insert.setGrpId(id);
            insert.setUserId(user.getId());
            insert.setJoinTime(LocalDateTime.now());
            grpDetailMapper.insert(insert);
            //通知该用户
            Map<String,String> userTokenMap = redisTemplate.opsForHash().entries("login");
            String token = userTokenMap.get(user.getUserName());
            if(token!=null){
                Map map=new HashMap();
                map.put("masterInviteMe",true);
                ObjectMapper objectMapper=new ObjectMapper();
                GetMyGrpResp getMyGrpResp=new GetMyGrpResp();
                getMyGrpResp.setId(id);
                getMyGrpResp.setNoRead(0);
                getMyGrpResp.setName(grp.getName());
                getMyGrpResp.setAvatar(grp.getAvatar());
                getMyGrpResp.setMasterId(localUser.getId());
                simpMessagingTemplate.convertAndSend("/u/"+token,objectMapper.writeValueAsString(getMyGrpResp),map);
            }
            return ResponseBean.success("",null);
        }

    }
}
